//
//  AudioBookViewController.m
//  FunRead
//
//  Created by  King on 15/5/26.
//  Copyright (c) 2015年 华东伟. All rights reserved.
//

#import "AudioBookViewController.h"
#import "SANetWorkingTask.h"
#import "AudioBookModel.h"
#import "AudioBookCollectionViewCell.h"
#import <SDWebImage/UIImageView+WebCache.h>
#import "AudioBookCatagoryViewController.h"
#import "MJRefresh.h"
@interface AudioBookViewController ()<UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout>
// 创建集合视图数组属性
@property (nonatomic, retain) NSMutableArray *collectArray;
// 创建collectView视图属性，用来reload数据
@property (nonatomic, retain) UICollectionView *collectView;

@end

@implementation AudioBookViewController
- (void)dealloc
{
    [_collectArray release];
    [_collectView release];
    [super dealloc];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // 设置导航跟tabBar透明
    self.navigationController.navigationBar.translucent = NO;
    self.tabBarController.tabBar.translucent = YES;

    // 设置导航返回键图片
    self.navigationController.navigationBar.tintColor = [UIColor grayColor];
    UIBarButtonItem *button = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"backbefore.png"] style:UIBarButtonItemStylePlain target:self action:@selector(action:)];
    self.navigationItem.leftBarButtonItem = button;
    [button release];
    
    // 设置页面背景图片
    UIImageView *backImage = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
    backImage.image = [UIImage imageNamed:@"back01.jpg"];
    [self.view addSubview:backImage];
    [backImage release];
    
    [self startConnection];
    [self createCollectionView];
    
    [self addHeader];

}
- (void)action:(id)back
{
    [self.navigationController popViewControllerAnimated:YES];
}

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // 初始化collectArray
        self.collectArray = [NSMutableArray array];
    }
    return self;
}
#pragma mark - 网络请求
- (void)startConnection
{
    
    __block AudioBookViewController *audioBook = self;
    // 调用类方法请求网络
    [SANetWorkingTask request:@"http://mobile.ximalaya.com/m/category_tag_list?category=book&device=iPad&type=album" parmater:nil block:^(NSDictionary *result) {
        audioBook.collectArray = [result objectForKey:@"list"];
        // collectView重新加载数据
        [self.collectView reloadData];
    }];
    
}
#pragma mark -上拉加载，下拉刷新
- (void)addHeader
{
    // 添加下拉刷新头部控件
    [self.collectView addHeaderWithCallback:^{
        // 进入刷新状态就会回调这个Block
            [self.collectView reloadData];
            // 结束刷新
            [self.collectView headerEndRefreshing];
    }];
#warning 自动刷新(一进入程序就下拉刷新)
    [self.collectView headerBeginRefreshing];
}


#pragma mark - 创建collectionView
- (void)createCollectionView
{
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.sectionInset = UIEdgeInsetsMake(10, 0, 10, 0);
    
    layout.itemSize = CGSizeMake((self.view.frame.size.width - 10) / 2 , (self.view.frame.size.height - 64 - 49) / 8);
    
   
    self.collectView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - 64 - 49) collectionViewLayout:layout];
    _collectView.backgroundColor = [UIColor colorWithWhite:0.5 alpha:0.5];
    _collectView.dataSource = self;
    _collectView.delegate = self;
    [_collectView registerClass:[AudioBookCollectionViewCell class] forCellWithReuseIdentifier:@"cell"];
    [self.view addSubview:_collectView];
    [layout release];
    [_collectView release];
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.collectArray.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    AudioBookCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    AudioBookModel *model = [[AudioBookModel alloc] init];
    NSDictionary *dic = [self.collectArray objectAtIndex:indexPath.row];
    [model setValuesForKeysWithDictionary:dic];
    
    [cell.coverImageView sd_setImageWithURL:[NSURL URLWithString:model.cover_path] placeholderImage:[UIImage imageNamed:@"1.jpg"]];
    cell.nameLabel.text = model.tname;
    [model release];
    
    
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    NSDictionary *dic = [self.collectArray objectAtIndex:indexPath.row];
    AudioBookCatagoryViewController *catagory = [[AudioBookCatagoryViewController alloc] init];
    catagory.name = [dic objectForKey:@"tname"];
    // 影藏tabbar, 回来时就又有了
    catagory.hidesBottomBarWhenPushed =YES;
    [self.navigationController pushViewController:catagory animated:YES];
    
    [catagory release];
    
    
}

@end
